from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.


# 基类
class BaseModel(models.Model):
    create_time = models.DateTimeField("创建时间", auto_now_add=True)
    update_time = models.DateTimeField("更新时间", auto_now=True)


# 部门类
class Department(BaseModel):
    name = models.CharField("部门", max_length=50)


    def __str__(self):
        return self.name

    class Meta:
        db_table = "department"


# 职责类
class Duty(BaseModel):
    name = models.CharField("工作职责", max_length=50)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "duty"


# 员工模型类
class Staff(models.Model):
    SEX = (
        (0, "男"),
        (1, "女")
    )
    name = models.CharField("姓名", max_length=30)
    sex = models.SmallIntegerField("性别", choices=SEX, default=0)
    phone = models.CharField("手机号", max_length=11)
    email = models.EmailField("邮箱", max_length=50)

    # 部门
    department = models.ForeignKey(Department, on_delete=models.CASCADE, verbose_name="部门")

    # 职责
    duty = models.ForeignKey(Duty, on_delete=models.CASCADE, verbose_name="职责")

    def __str__(self):
        return self.name

    class Meta:
        db_table = "staff"


# 权限类型（角色）
# name varchar(30)  '权限类型的名称'
class PermissionType(models.Model):
    """权限的类型"""
    name = models.CharField(max_length=30, verbose_name='权限类型的名称')

    class Meta:
        db_table = 'permission_type'
        verbose_name_plural = "权限类型"

    def __str__(self):
        return self.name


# 用户
class User(AbstractUser):

    phone = models.CharField("手机号", max_length=11, unique=True, null=True, blank=True)
    last_login = models.DateTimeField("上次登录", null=True, blank=True)
    # 用户的角色
    role = models.ManyToManyField(PermissionType)

    def __str__(self):
        return self.username

    class Meta:
        db_table = "user_tb"
        verbose_name_plural = "用户表"


# 用户头像的视图
class UserPortrait(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="所属用户")
    avatar = models.CharField("头像", max_length=200)

    def __str__(self):
        return "%s: %s"%(self.user.username, self.avatar)

    class Meta:
        db_table = "user_portrait"
        verbose_name_plural = "用户头像表"


# 权限表
# name  varchar(30)  '权限名称'
# code_name  varchar(300)  '权限识别名称'
# type_name fk  --->PermissionType
class Permission(models.Model):
    """
    权限模型类
    """
    name = models.CharField(max_length=30, verbose_name='权限名称')
    code_name = models.CharField(max_length=300, verbose_name='权限识别名称')
    type_name = models.ForeignKey(PermissionType, on_delete=models.CASCADE, verbose_name="角色")

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'permission'
        verbose_name_plural = "用户权限"


# 用户组
# name  varchar(30)  '组名称'
# user  m:n
# permission m:n
class UserGroup(models.Model):
    """用户组的模型类"""
    name = models.CharField(max_length=30, verbose_name='组名称')
    user = models.ManyToManyField(User)
    # 更改模型类: 改为多对多
    permission = models.ManyToManyField(Permission)

    class Meta:
        db_table = 'user_group'
        verbose_name_plural = "用户组"

    def __str__(self):
        return self.name
